********************************************************************************
* Data preparation: Tax and transfer variables from the CPS directly
********************************************************************************

include choose_version.do

* ctccrd: child tax credit
* individual level variable
replace ctccrd = . if ctccrd == 999999
bysort serial: egen ctccrd_hh = total(ctccrd), missing
label var ctccrd_hh "total HH child tax credit, aggregated from individual"

* actccrd: additional child tax credit
* individual level variable
replace actccrd = . if actccrd == 99999
bysort serial: egen actccrd_hh = total(actccrd), missing
label var actccrd_hh "total HH additional child tax credit, aggregated from individual"

* eitcred: earned income tax credit
* individual level variable
replace eitcred = . if eitcred == 9999
bysort serial: egen eitcred_hh = total(eitcred), missing
label var eitcred_hh "total HH earned income tax credit, aggregated from individual"

* fedtax: federal income tax liability, before credits (additional child tax credit and earned income tax credit)
* individual level variable
replace fedtax = . if fedtax == 99999999
bysort serial: egen fedtax_hh = total(fedtax), missing
label var fedtax_hh "total HH BC federal income tax liability, aggregated from individual"

* fedtaxac: federal income tax liability, after credits
* individual level variable
replace fedtaxac = . if fedtaxac == 99999999
bysort serial: egen fedtaxac_hh = total(fedtaxac), missing
label var fedtaxac_hh "total HH AC federal income tax liability, aggregated from individual"

* fica: total Social Security retirement payroll deductions
* individual level variable
replace fica = . if fica == 99999
bysort serial: egen fica_hh = total(fica), missing
label var fica_hh "total HH social security payroll deduction"

* fedretir: federal retirement payroll deduction
* individual level variable
replace fedretir = . if fedretir == 999999
bysort serial: egen fedretir_hh = total(fedretir), missing
label var fedretir_hh "total HH federal retirement payroll deduction"

* statetax: state income tax liability, before credits
* individual level variable
replace statetax = . if statetax == 9999999
bysort serial: egen statetax_hh = total(statetax), missing
label var statetax_hh "total HH BC state income tax liability, aggregated from individual"

* stataxac: state income tax liability, after credits
* individual level variable
replace stataxac = . if stataxac == 9999999
bysort serial: egen stataxac_hh = total(stataxac), missing
label var stataxac_hh "total HH AC state income tax liability, aggregated from individual"

* dividend tax
gen divtax_hh = 0.133*incdivid_hh 
label var divtax_hh "Imputed dividend tax; to be deducted from tax liability"

********************************************************************************
* Generate own tax and transfer variables
********************************************************************************

* federal credits
if `version_robust' == 4 {
	*egen credits = rowtotal(actccrd eitcred ctccrd), missing
	gen credits = 0
	replace credits = fedtax - fedtaxac + ctccrd
}
else if `version_robust' == 5 {
	*egen credits = rowtotal(actccrd eitcred), missing
	gen credits = 0 
	replace credits = fedtax - fedtaxac
}
else {
	gen credits = 0
	replace credits = -fedtaxac if fedtaxac < 0
}
label var credits "federal credits"
bysort serial: egen credits_hh = total(credits), missing
label var credits_hh "HH federal credits, aggregated from individual"

* federal tax after credits if positive variable 
gen fedtaxac_pos = 0
replace fedtaxac_pos = fedtaxac if fedtaxac >= 0
bysort serial: egen fedtaxac_pos_hh = total(fedtaxac_pos), missing

* state credits
if `version_robust' == 4 {
	gen state_credits = statetax - stataxac
}
else {
	gen state_credits = 0
	replace state_credits = -stataxac if stataxac < 0
}
label var state_credits "difference between BC and AC state taxes"
bysort serial: egen state_credits_hh = total(state_credits), missing
label var state_credits_hh "household level difference between BC and AC state taxes"

* state tax after credits if positive variable 
gen stataxac_pos = 0
replace stataxac_pos = stataxac if stataxac >= 0
bysort serial: egen stataxac_pos_hh = total(stataxac_pos), missing

* Construct FICA employer for 2012
* OASDI: Social Security's Old-Age, Survivors, and Disability Insurance: 4.2% employee; 6.2% employer; limit 110,100
* HI: 1.45% employee; 1.45% employer; no limit
gen incwage_fica = incwage if incwage < 110100															// upper bound for OASDI part
replace incwage_fica = 110100 if incwage >= 110100 & incwage != .										// upper bound for OASDI part
gen fica_wage = (0.0359+0.0061)*incwage_fica + 0.0145*incwage											// employee part (computed on wage and salary income)
gen fica_employer = fica_wage																			// set employer part equal to employee part just computed
replace fica_employer = fica if fica_wage > fica & fica_wage != . & fica != .							// adjust if what we just computed is larger than employee part
*replace fica_employer = fica_employer*0.0765/0.0565 if fica_employer <= 6220.65							// scale up because employer rate is higher
*replace fica_employer = (6220.65*0.0765/0.0565 + fica_employer-6220.65) if fica_employer > 6220.65		// scale up because employer rate is higher (but take into account income limit)
if fica_employer <= 6220.65 {
	replace fica_employer = fica_employer*0.0765/0.0565
}
else if fica_employer > 6220.65 & fica_employer != . {
	replace fica_employer = (6220.65*0.0765/0.0565 + fica_employer-6220.65) if fica_employer > 6220.65
}
label var fica_employer "social security contribution of employer"
bysort serial: egen fica_employer_hh = total(fica_employer), missing									// aggregate at household level
label var fica_employer_hh "social security contribution of employer (HH)"